<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="../../js/boot.js"></script>
</head>

<body>
    <div class="layui-fluid">
        <div class="flexrow">
            <div class="layui-panel list-left-container" style="width: 200px;">
                <ul id="datatree" class="ztree"></ul>
            </div>
            <div class="layui-panel list-right-container">
                <div class="list-search-container">
                    <div class="list-search-item">
                        <span>菜单名称：</span>
                        <input id="menu_name" autocomplete="off" class="layui-input">
                    </div>
                    <button class="layui-btn layui-btn-sm" style="margin-left:5px" onclick="BindData()"><i class="layui-icon layui-icon-search layui-font-12"></i>搜索</button>
                </div>
                <table class="layui-table" lay-data="{id: 'datagrid'}" lay-filter="datagrid">

                </table>
            </div>
        </div>
    </div>
</body>
<script type="text/html" id="op">
    <button class="layui-btn layui-btn-sm" lay-event="HandleEdit"><i class="layui-icon layui-icon-edit layui-font-12"></i>设置</button>
</script>
<script type="text/html" id="role">
    <div>
        {{# layui.each(d.roleList, function(index, item){ }}
            {{# if(item.role_name === d.LAY_COL.title){ }}
                {{# if(item.regist){ }}
                    <input type="checkbox" lay-filter="rolesetting" role_id={{ item.row_id }} menu_id={{ d.row_id }} lay-skin="primary" checked />
                {{# } else { }}
                        <input type="checkbox" lay-filter="rolesetting" role_id={{ item.row_id }} menu_id={{ d.row_id }} lay-skin="primary" />
                {{# } }}
             {{# } }}
         {{# }); }}
    </div>
</script>
<script>
   var menu_code = "";

    $(function() {
        BindData();
        BindSettingClick();
    })

    function BindData() {
        BindTableData();
        BindTreeData();
    }

    function BindTableData() {
        HttpPost(JSON.stringify({}), 'system/org/role/getAllRoleList', function(result) {
            if (result.code == 0) {
                var array = [];
                array.push({ title: '菜单名称', field: 'menu_name', width: 250 });
                array.push({ title: "完全公开", fixed: 'right', align: 'center', toolbar: '#role' });
                result.data.obj.forEach(element => {
                    array.push({ title: element.role_name, fixed: 'right', align: 'center', toolbar: '#role' });
                });
                array.push({ title: '操作', fixed: 'right', width: 100, align: 'center', toolbar: '#op' });
                var cols = [array];
                var param = {
                    menu_name: $("#menu_name").val(),
                    menu_code: menu_code
                }
                BindTableV2("datagrid", cols, "system/setting/menusetting/getMenuSettingList", param);
            }
        })

    }


    function BindTreeData() {
        BindTree("datatree", "system/setting/menu/tree", null, TreeNodeClick);
    }

    function TreeNodeClick(event, treeId, treeNode, clickFlag) {
        menu_code = treeNode.id;
        BindTableData();
    }

    function HandleEdit(RowData) {
        OpenDialog('菜单设置', 'system/menu/menu_setting_edit.html?row_id=' + RowData.row_id, 1000, 800, function() {
            BindData();
        })
    }

    function BindSettingClick() {
        layui.form.on('checkbox(rolesetting)', function(data) {
            var checkbox = data.elem;
            var menu_id = $(checkbox).attr("menu_id");
            var role_id = $(checkbox).attr("role_id");
            var regist = $(checkbox).prop('checked');
            var param = {
                menu_id: menu_id,
                role_id: role_id,
                regist: regist
            }
            HttpPost(JSON.stringify(param), 'system/setting/menusetting/updateSetting', function(result) {
                if (result.code == 0) {
                    OpenSuccessMessage("设置成功")
                    BindTableData();
                } else {
                    OpenFail(result.msg);
                }
            })
        });
    }
</script>

</html>